package smartgrid.modules.normative.norm;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import smartgrid.modules.normative.sanction.SanctionAbstract;
import smartgrid.modules.trade.Contract;

public class Norm {
	
	public enum Status {
		UNKNOWN,
		COMPLIED,
		VIOLATED;
	}
	
	// Applicable sanctions depending whether the agent cooperated or violated the
	// norm
	private Map<Status, List<SanctionAbstract>>	sanctions;
	
	
	public Norm(Map<Status, List<SanctionAbstract>> sanctions) {
		this.sanctions = sanctions;
	}
	
	
	public List<SanctionAbstract> getSanctions(Status normStatus) {
		if (this.sanctions.containsKey(normStatus)) {
			return this.sanctions.get(normStatus);
		}
		
		return new ArrayList<SanctionAbstract>();
	}
	
	
	public static Status evaluateContract(Contract contract) {
		if ((contract.getQtyConsumed() <= contract.getQtySupplied())
				|| (contract.getQtySupplied() == contract.getQtyOffered())) {
			return Status.COMPLIED;
		} else {
			return Status.VIOLATED;
		}
	}
	
	
	public static double getMagnitude(Contract contract) {
		double magnitude = 0.0;
		
		if (contract.getQtyConsumed() > contract.getQtySupplied()) {
			
			magnitude = (Math.min(contract.getQtyDemanded(),
					contract.getQtyConsumed()) - contract.getQtySupplied())
					/ Math.min(contract.getQtyDemanded(), contract.getQtyConsumed());
			
		}
		
		return magnitude;
	}
}